"""
# 处理大型分类变量：分箱计数
"""
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('../数据集/train_subset.csv')
# 看看训练集中有多少唯一的特征
print(len(df.device_id.unique()))


# 对于每个类别，我们要计算
# Theta = [counts, p(click), p(no click), p(click)/p(no click)]

def click_counting(x, bin_column):
    clicks = pd.Series(x[x['click'] > 0][bin_column].value_counts(), name='clicks')
    no_clicks = pd.Series(x[x['click'] < 1][bin_column].value_counts(), name='no_clicks')

    counts = pd.DataFrame([clicks, no_clicks]).T.fillna('0')

    counts['total_clicks'] = counts['clicks'].astype('int64') + counts['no_clicks'].astype('int64')

    return counts


def bin_counting(counts):
    counts['N+'] = counts['clicks'].astype('int64').divide(counts['total_clicks'].astype('int64'))
    counts['N-'] = counts['no_clicks'].astype('int64').divide(counts['total_clicks'].astype('int64'))
    counts['log_N+'] = counts['N+'].divide(counts['N-'])
    bin_counts = counts.filter(items=['N+', 'N-', 'log_N+'])
    return counts, bin_counts


# 分箱计数示例：device_id
bin_column = 'device_id'
device_clicks = click_counting(df.filter(items=[bin_column, 'click']), bin_column)
device_all, device_bin_counts = bin_counting(device_clicks)

# 检查一下，确定我们处理了所有设备
print(len(device_bin_counts))

print(device_all.sort_values(by='total_clicks', ascending=False).head(4))
